package service.timedTask;

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.support.WebApplicationContextUtils;

import entities.menu.MenuEntities;
import service.menu.impl.MenuServiceImpl;
/**
 * 监听器实现定时任务
 * @author hxl
 *
 */
public class TestTask implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

	@Override
	public void contextInitialized(final ServletContextEvent sce) {
		TimerTask task=new TimerTask(){
			@Override  
            public void run() {  
                // task to run goes here  
                System.out.println("我是监听器获得spring容器的数据的测试"); 
                MenuServiceImpl menuServiceImpl=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(MenuServiceImpl.class);
                MenuEntities mes=menuServiceImpl.getAllMenu();
                System.out.println(mes.getText());
            } 
		};
		Timer timer=new Timer();
		timer.scheduleAtFixedRate(task, 0, 1000);
	}
}
